<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
use app\index\model\VerCode;
use \think\Loader;
use \think\File;
class Edit extends Controller
{   
    // 从数据库中取出当前用户资料并加载模版
    public function index()
    {
        $ques = (new VerCode)->getRandOne();
        $pid = input('pid');



        $data = Db::table('post')->join('cate c','post.class_id = c.cid')->find($pid);
        if ($data['uid']!=session('uid')) {
            $this->error('非法请求');
            # code...
        }
        // var_dump($data);
        return $this->fetch('',['ques'=>$ques,'data'=>$data]);
    }

    public function edit(){
    	$data = input('post.');


    	if ($data['vercode']<>session('answer')) {
    		return ['error'=>0,'info'=>'验证码不正确,请重新输入'];
    	}

    	// $data = ['class_id'=>$data['class_id'],'title'=>$data['title'],'content'=>$data['content'],'kiss'=>$data['kiss'],'uid'=>session('uid'),'ctime'=>time()];
    	$data['ctime'] = time();
    	$data['uid'] = session('uid');
        // Db::table('user')->where('uid',session('uid'))->setDec('kiss',$data['kiss']);
    	$res = Db::table('post')->where('pid',$data['pid'])->update(['content'=>$data['content']]);

    	if($res){
            return ['error'=>0,'info'=>'发表成功','url'=>url('index/Read/index',['pid'=>$data['pid']])];
        }else{
            return ['error'=>1,'info'=>'发表失败'];
        }
    }

    // public function readPost(){

    // 	return $this->fetch('');
    // }

}


 ?>